package main

import (
	"alarm/dao"
	"alarm/models"
	"alarm/routers"
	"alarm/service"
	"alarm/setting"
	"fmt"
	"os"
	"time"
)

func main1() {
	if len(os.Args) < 2 {
		fmt.Println("Usage：./alarm conf/config.ini")
		return
	}
	// 加载配置文件
	if err := setting.Init(os.Args[1]); err != nil {
		fmt.Println("load config from file failed, err:%v\n", err)
		return
	}
	// 创建数据库
	// sql: CREATE DATABASE alarm;
	// 连接数据库
	err := dao.InitMySQL(setting.Conf.MySQLConfig)
	if err != nil {
		fmt.Printf("init mysql failed, err:%v\n", err)
		return
	}
	defer dao.Close() // 程序退出关闭数据库连接
	// 模型绑定
	dao.DB.AutoMigrate(&models.Todo{}, &models.Alarm{})
	// 注册路由
	r := routers.SetupRouter()
	if err := r.Run(fmt.Sprintf(":%d", setting.Conf.Port)); err != nil {
		fmt.Println("server startup failed, err:%v\n", err)
	}
}

func main2() {
	if len(os.Args) < 2 {
		fmt.Println("Usage：./alarm conf/config.ini")
		return
	}
	// 加载配置文件
	if err := setting.Init(os.Args[1]); err != nil {
		fmt.Println("load config from file failed, err:%v\n", err)
		return
	}
	// 创建数据库
	// sql: CREATE DATABASE alarm;
	// 连接数据库
	err := dao.InitMySQL(setting.Conf.MySQLConfig)
	if err != nil {
		fmt.Printf("init mysql failed, err:%v\n", err)
		return
	}
	defer dao.Close() // 程序退出关闭数据库连接
	// 模型绑定
	//alarm := new(models.Alarm)
	//if err = dao.DB.Debug().Where("id=?", 1).First(alarm).Error; err != nil {
	//		fmt.Println("xxx", err)
	//}

	//if err = dao.DB.Debug().First(alarm, 10).Error; err != nil {
	//	fmt.Println("xxx1", err)
	//}
	var alarms []*models.Alarm
	if err = dao.DB.Debug().Limit(3).Offset(1).Find(&alarms).Error; err != nil {
		fmt.Println("limit", err)
	}
	type Result struct {
		ID         int
		SystemType string
	}

	var results []Result
	// 原生 SQL
	if err = dao.DB.Debug().Raw(`
	select alarm_info.system_type,service.label service_type_desc from (
		-- -----
		select alarm.*,b.master_code,b.sub_code,b.system_type,b.severity 
		from 
		(
		select a.*,b.count from alarm_record a right join (
		select alarm_definition_id,max(updated_time) updated_time,count(1) count from alarm_record group by alarm_definition_id)b
		on a.updated_time = b.updated_time and a.alarm_definition_id = b.alarm_definition_id
		) alarm left join 
		alarm_definition b on alarm.alarm_definition_id = b.id
		-- -----
		) alarm_info left join
		(select * from dict_detail where dict_id = 13)  service 
		on alarm_info.system_type = service.id 
	`).Scan(&results).Error; err != nil {
		fmt.Println("limit-->", err)
	}
	go tickDemo()
	fmt.Printf("%#v\n", results)

}

func tickDemo() {
	ticker := time.Tick(time.Minute) //定义一个1秒间隔的定时器
	for i := range ticker {
		fmt.Println(i) //每秒都会执行的任务
	}
}

func main() {
	if len(os.Args) < 2 {
		fmt.Println("Usage：./alarm conf/config.ini")
		return
	}
	// 加载配置文件
	if err := setting.Init(os.Args[1]); err != nil {
		fmt.Println("load config from file failed, err:%v\n", err)
		return
	}
	// 创建数据库
	// sql: CREATE DATABASE alarm;
	// 连接数据库
	err := dao.InitMySQL(setting.Conf.MySQLConfig)
	if err != nil {
		fmt.Printf("init mysql failed, err:%v\n", err)
		return
	}
	defer dao.Close() // 程序退出关闭数据库连接

	list, err := service.LoadActiveAlarmList(26)
	fmt.Printf("%#v---------\n", list)

	for index, value := range list {
		fmt.Println(index, value)
		fmt.Printf("index:d% value:%#v \n", index, value)
		fmt.Printf("index:d% value:%#v \n", index, *value)
	}

}
